Skip to content

Conversation

lihaoyi
Copy link
Contributor

@lihaoyi lihaoyi commented Oct 2, 2025

This ports over one of the other Ammonite features to the Scala 3 REPL, making it behave much more similarly to other prompt environment (Bash, Python, etc.):

  • If there is no code running, Ctrl-C just resets the prompt without exiting
  • If there is code running, Ctrl-C first interrupts the thread to try and force a gentle exit. A second Ctrl-C would then terminate the process.

Unfortunately we will no longer be able to terminate the thread forcefully, as Thread.stop is busted Java >20 (see com-lihaoyi/Ammonite#1379), so using Thread.interrupt and sys.exit is the best we have

Tested manually using bin/scala. Doesn't work in sbt repl due to SBT also intercepting the signal, but that's a separate issue we can follow up later

@bishabosha
Copy link
Member

bishabosha commented Oct 2, 2025

scala> while true do
     |   Thread.sleep(1000)
     |   println("still alive!")
     |
still alive!
still alive!
^C
Interrupting running thread, Ctrl-C again to terminate the REPL Process
java.lang.InterruptedException: sleep interrupted
  at java.base/java.lang.Thread.sleep(Native Method)
  ... 32 elided

scala>

Nice!

@bishabosha
Copy link
Member

bishabosha commented Oct 2, 2025

the double Ctl+c doesn't quit for me but keeps resetting the prompt

Edit: it has to be a true infinite loop, with no yielding, for double interrupt to work (so no sleep)

@lihaoyi
Copy link
Contributor Author

lihaoyi commented Oct 2, 2025

Ctrl-C only exits the process when it is running code that it is unable to interrupt. Doesn't need to be infinite, as long as it's running: the first Ctrl-C will ask for confirmation, the second Ctrl-C will exit the process

Copy link
Contributor

@Gedochao Gedochao left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Gedochao
Copy link
Contributor

Gedochao commented Oct 8, 2025

I wonder if/how we could test this in a meaningful manner... can be as a follow-up, I think.
I'm leaving the PR open for a bit, so that there's room for a second opinion (@bracevac)

@Gedochao Gedochao requested a review from tgodzik October 9, 2025 14:10
Copy link
Contributor

@tgodzik tgodzik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@bracevac bracevac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, LGTM!

Co-authored-by: Oliver Bračevac <bracevac@users.noreply.github.com>
@bracevac bracevac merged commit 7b7f2f1 into scala:main Oct 9, 2025
51 checks passed
@lihaoyi
Copy link
Contributor Author

lihaoyi commented Oct 9, 2025

@bracevac you merged it without squashing, you probably should force push and clean up the history before it gets buried under further commits otherwise the git commit history is going to be full of trash

bracevac added a commit that referenced this pull request Oct 10, 2025
…f some command is running ask for confirmation before exiting (#24127)

This ports over one of the other Ammonite features to the Scala 3 REPL,
making it behave much more similarly to other prompt environment (Bash,
Python, etc.):

- If there is no code running, `Ctrl-C` just resets the prompt without
exiting
- If there is code running, `Ctrl-C` first interrupts the thread to try
and force a gentle exit. A second `Ctrl-C` would then terminate the
process.

Unfortunately we will no longer be able to terminate the thread
forcefully, as `Thread.stop` is busted Java >20 (see
com-lihaoyi/Ammonite#1379), so using
`Thread.interrupt` and `sys.exit` is the best we have

Tested manually using `bin/scala`. Doesn't work in `sbt repl` due to SBT
also intercepting the signal, but that's a separate issue we can follow
up later
@bracevac
Copy link
Contributor

Sorry about that! The cascade of approvals made me not look too closely. We can't change it now. That reminds me: I'm not aware what's our official etiquette for PRs. Perhaps, an accompanying message that communicates if merging is desired or not would be nice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants